package com.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.common.dto.PagingReqDTO;
import com.dao.RepoMapper;
import com.entity.Repo;
import com.entity.dto.RepoDTO;
import com.entity.dto.request.RepoReqDTO;
import com.entity.dto.response.RepoRespDTO;
import com.service.RepoService;
import com.utils.PageUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 题库
 * </p>
 */
@Service
public class RepoServiceImpl extends ServiceImpl<RepoMapper, Repo> implements RepoService {

    @Override
    public PageUtils paging(PagingReqDTO<RepoReqDTO> reqDTO) {
        Page page = new Page();
        page.setCurrent(reqDTO.getCurrent());
        page.setSize(reqDTO.getSize());


        int pagingCount = baseMapper.pagingCount(reqDTO.getParams());
        page.setTotal(pagingCount);
        if (pagingCount > 0) {
            List<RepoRespDTO> paging = baseMapper.paging(reqDTO.getParams());
            page.setRecords(paging);
        } else {
            page.setRecords(Collections.emptyList());
        }
        return new PageUtils(page);
    }

    @Override
    public void save(RepoDTO reqDTO) {

        //复制参数
        Repo entity = new Repo();
        BeanUtils.copyProperties(reqDTO, entity);
        if (entity.getCreateTime() == null) {
            entity.setCreateTime(new Date());
        }
        entity.setUpdateTime(new Date());
        this.insertOrUpdate(entity);
    }
}
